package com.wdh.kjs.Adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wdh.kjs.R;
import com.wdh.kjs.activity.MessageEditActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by dingh on 2016/4/20.
 */

public class MessageEditAdapter extends BaseAdapter  {

    private List<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
    private Context context;
    private HashMap<String, String> maps = new HashMap<String, String>();
    private ViewHolder viewHolder;
    public MessageEditAdapter(Context context, List<HashMap<String, String>> data) {
        this.data = data;
        this.context = context;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(context, R.layout.item_station_message_edit, null);
            viewHolder.tv_message_edit_title = (TextView) convertView.findViewById(R.id.tv_message_edit_title);
            viewHolder.tv_message_edit_time = (TextView) convertView.findViewById(R.id.tv_message_edit_time);
            viewHolder.cb_message = (CheckBox) convertView.findViewById(R.id.cb_message);
            viewHolder.edit_red = (ImageView) convertView.findViewById(R.id.edit_red);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        maps = data.get(position);
        if (maps != null) {
            viewHolder.tv_message_edit_title.setText(maps.get("tv_message_title"));
            viewHolder.tv_message_edit_time.setText(maps.get("tv_message_time"));
        }
        if(maps.get("userMessageState").equals("1")){
            viewHolder.edit_red.setBackgroundResource(R.drawable.bt_gray_oval_solid);
        }
        ItemListener itemListener = new ItemListener(position);


        viewHolder.cb_message.setOnCheckedChangeListener(itemListener);

        return convertView;
    }



    class ViewHolder {
        TextView tv_message_edit_title;
        TextView tv_message_edit_time;
        CheckBox cb_message;
        ImageView edit_red;


    }
    private class ItemListener implements CompoundButton.OnCheckedChangeListener {

        private int m_position;
        public ItemListener(int position) {
            this.m_position = position;
        }

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {

                Toast.makeText(context, data.get(m_position).get("userReceiveMessageId"), Toast.LENGTH_SHORT).show();
                MessageEditActivity.list.add(data.get(m_position).get("userReceiveMessageId"));
            }
            else {
               for (int i=0;i<MessageEditActivity.list.size();i++) {
                   if (MessageEditActivity.list.get(i).equals(data.get(m_position).get("userReceiveMessageId")))
                   MessageEditActivity.list.remove(i);
               }
               }
//                    MessageEditActivity.list.remove(data.get(m_position).get("userReceiveMessageId"));
            Log.i("ddd"," "+MessageEditActivity.list+"");

            }

        }

//        @Override
//        public void onClick(View v) {
//                if(viewHolder.cb_message.isChecked()){
//                    Log.i("msg","userReceiveMessageId "+maps.get("userReceiveMessageId"));
//                    Toast.makeText(context, data.get(m_position).get("userReceiveMessageId"), Toast.LENGTH_SHORT).show();
//                }
//        }
    }
